package com.konsung.ksdc.common.widget;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.konsung.ksdc.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 型号分类弹出框
 * Created by Administrator on 2016/5/18 0018.
 */
public class ProductTypeBottomPop extends PopupWindow implements View.OnClickListener {
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_remain_num)
    TextView tvRemainNum;
    @Bind(R.id.tv_subtitle)
    TextView tvType;
    @Bind(R.id.rg_type_container)
    FlowRadioGroup rgTypeContainer;
    @Bind(R.id.net_count)
    NumEditText netCount;
    @Bind(R.id.ll_net_container)
    LinearLayout llNetContainer;
    @Bind(R.id.dialog_bookshelf_context_rl)
    FrameLayout dialogBookshelfContextRl;
    @Bind(R.id.btn_close)
    Button btnClose;

    private Activity activity;
    private final PopupWindow popWindow;

    /**
     * 传递所依附的activity和 背景view
     * @param activity activity
     * @param viewBg view
     */
    public ProductTypeBottomPop(final Activity activity, final View viewBg) {
        this.activity = activity;
        View view = View.inflate(activity, R.layout.dialog_product_type, null);
        ButterKnife.bind(this, view);
        netCount.setEditEnable(false);
        btnClose.setOnClickListener(this);

        // 最后一个参数false 代表：不与其余布局发生交互， true代表：可以与其余布局发生交互事件
        popWindow = new PopupWindow(view,
                WindowManager.LayoutParams.FILL_PARENT, WindowManager
                .LayoutParams.WRAP_CONTENT, false) {
            // 重写popupWindow消失时事件
            @Override
            public void dismiss() {
                // 在pop消失之前,view设置背景渐变出场动画
                viewBg.startAnimation(AnimationUtils.loadAnimation(activity,
                        R.anim.anim_product_type_editer_exit));
                viewBg.setVisibility(View.GONE);
                super.dismiss();
            }
        };
        // 设置Pop入场动画效果
        popWindow.setAnimationStyle(R.style.ProductTypePopAnim);
        // 设置Pop响应内部区域焦点
        popWindow.setFocusable(true);
        // 设置Pop响应外部区域焦点
        popWindow.setOutsideTouchable(false);
        // 设置PopupWindow弹出软键盘模式（此处为覆盖式）
        popWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popWindow.setSoftInputMode(WindowManager.LayoutParams
                .SOFT_INPUT_ADJUST_PAN);
        // 响应返回键必须的语句
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        // 在显示pop之前，给咱们加的view设置背景渐变入场动画（Android3
        // .0以上的开发环境，这里建议使用属性动画，那样很柔和，视觉效果更棒！）
        viewBg.setVisibility(View.VISIBLE);
        viewBg.startAnimation(AnimationUtils.loadAnimation(activity,
                R.anim.anim_product_type_editer_enter));
        // 依附的父布局自己设定，我这里为了方便，这样写的。
        popWindow.showAtLocation(viewBg, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_close:
                if (popWindow != null) {
                    popWindow.dismiss();
                }
                break;
            default:
                break;
        }
    }
}
